﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace Easy.Web.Service.Attachment
{
    /// <summary>
    /// AddAttachment 的摘要说明
    /// </summary>
    public class AddAttachment : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string msg;
            try
            {
                HttpPostedFile file = context.Request.Files[0];
                string fileFolderPath = context.Request.Form["path"];

                if (!file.Equals(null))
                {
                    string filename = Path.GetFileName(file.FileName);
                    string extension = Path.GetExtension(filename).ToLower();

                    List<string> upLoadExtensions = new List<string>
                    {
                        ".gif", ".jpg", ".jpeg", ".png", ".amr", ".wav"
                    };

                    //gif、jpg、jpeg、png、apm
                    if (upLoadExtensions.Any(e => e == extension))
                    {
                        int filelength = file.ContentLength;
                        if (filelength <= 1*1024*1024*10)
                        {
                            string url;
                            switch (fileFolderPath)
                            {
                                case "im":
                                    url = string.Format("/uploadfiles/im/{0}/{1}/", DateTime.Now.ToString("yyyy-MM-dd"), 
                                        context.User.Identity.Name);
                                    break;
                                default :
                                    url = "/uploadfiles/attachment/" + context.User.Identity.Name + "/";
                                    break;
                            }
                            Directory.CreateDirectory(Path.GetDirectoryName(context.Server.MapPath(url)));
                            string path = url + DateTime.Now.ToString("yyyyMMddHHssmm") + extension;
                            file.SaveAs(context.Server.MapPath(path));
                            msg = "{\"Result\":\"" + path + "\"}";
                        }
                        else
                        {
                            msg = "{\"ErrorCode\":\"错误代码待定\",\"ErrorMessage\":\"图片过大\"}";
                        }
                    }
                    else
                    {
                        msg = "{\"ErrorCode\":\"错误代码待定\",\"ErrorMessage\":\"后缀名有误\"}";
                    }                  
                }
                else
                {
                    msg = "{\"ErrorCode\":\"错误代码待定\",\"ErrorMessage\":\"请选择文件\"}";
                }
            }
            catch (Exception ex)
            {
                msg = "{\"ErrorCode\":\"错误代码待定\",\"ErrorMessage\":\"" + ex.Message.Replace("\"", "“") + "\"}";
            }
            context.Response.Write(msg);
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}